package com.efeichong.project.domain.entity.db;

import com.efeichong.audit.InsertDefault;
import com.efeichong.audit.UpdateDefault;
import com.efeichong.common.config.AuditorHolder;
import com.efeichong.common.domain.BaseEntity;
import com.efeichong.logicDelete.LogicDelete;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @author
 * @date 2020/11/25
 * @description 角色
 */
@Setter
@Getter
@Entity
@Table
@LogicDelete(column = "hasDel")
@DynamicInsert
@DynamicUpdate
public class SysRole extends BaseEntity {
    /**
     * 创建人
     */
    @InsertDefault(val = AuditorHolder.class)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "[create_by]")
    private SysUser createBy;

    /**
     * 更新者
     */
    @UpdateDefault(val = AuditorHolder.class)
    @JoinColumn(name = "[update_by]")
    @ManyToOne(fetch = FetchType.LAZY)
    private SysUser updateBy;

    /**
     * 角色编码
     */
    @Column(name = "[code]", columnDefinition = "varchar(255)  comment'角色编码'")
    private String code;

    /**
     * 角色名
     */
    @Column(name = "[name]", columnDefinition = "varchar(255)  comment'角色名'")
    private String name;

    /**
     * 备注
     */
    @Column(name = "[remark]", columnDefinition = "varchar(255)  comment'备注'")
    private String remark;

    /**
     * 菜单
     */
    @JoinTable(name = "sys_role_menu_ship",
            joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "menu_id", referencedColumnName = "id"))
    @ManyToMany(fetch = FetchType.LAZY)
    @ApiModelProperty(value = "")
    private List<SysMenu> sysMenus;

    /**
     * 用户
     */
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "sysRoles")
    private List<SysUser> users = new ArrayList<>();

    /**
     * 是否删除 0未删除
     */
    @Column(name = "[has_del]", columnDefinition = "bigint default'0' comment'是否删除 0未删除'")
    private Long hasDel = 0L;
}
